<?php 
mysql_connect("127.0.0.1:3306","root","");
mysql_select_db("lishen");
mysql_query("set names utf8");

/*查找的条件*/

$str = "";

$type = $_POST['type'];

if($type == "add"){
    $arr = array();
    $name = $_POST['name'];
    $author = $_POST["author"];
    $price = $_POST['price'];
    mysql_query("insert into books (name,author,price) values ('$name','$author','$price')");
    $sql = "select * from books ".str;
    $result = mysql_query($sql);
    if(mysql_num_rows($result)){
        while($row = mysql_fetch_assoc($result)){
            $arr[] = $row;
        }
    }
    echo json_encode($arr);
}
if($type == "look"){
    $arr = array();
    $str = "";
    $sql = "select * from books ".$str;
    $result = mysql_query($sql);
    if(mysql_num_rows($result)){
        while($row = mysql_fetch_assoc($result)){
            $arr[] = $row;
        }
    }
    echo json_encode($arr);
}
if($type == "dete"){
    $arr = array();
    $name = $_POST["name"];
    $author = $_POST['author'];
    $price = $_POST["price"];
    mysql_query("delete from books where name = '$name' and author = '$author' and price = '$price'");
    $sql = "select * from books ".$str;
    $result = mysql_query($sql);
    if(mysql_num_rows($result)){
        while($row = mysql_fetch_assoc($result)){
            $arr[] = $row;
        }
    }
    echo json_encode($arr);
}
if($type == "mod"){
    $arr = array();
    $name = $_POST["name"];
    $author = $_POST['author'];
    $price = $_POST["price"];
    
    $newName = $_POST["newName"];
    $newAuthor = $_POST['newAuthor'];
    $newPrice = $_POST["newPrice"];
    
    mysql_query("update books set name = '$newName' where  name = '$name' and author = '$author' and price = '$price'");
    mysql_query("update books set author = '$newAuthor' where  name = '$newName' and author = '$author' and price = '$price'");
    mysql_query("update books set price = '$newPrice' where  name = '$newName' and author = '$newAuthor' and price = '$price'");
    $sql = "select * from books ".$str;
    $result = mysql_query($sql);
    if(mysql_num_rows($result)){
        while($row = mysql_fetch_assoc($result)){
            $arr[] = $row;
        }
    }
    echo json_encode($arr);
}

if($type == "search"){
    $arr = array();
    $str = "";
    $name = $_POST["name"];
    $author = $_POST['author'];
    $price = $_POST["price"];
    if($name == "*" && $author == "*" && $price == "*"){
        $str = "";
    }else{
        $str = "where ";
    }
    if($name != "*"){
        $str .= "name = '$name' & ";
    }
    if($author != "*"){
        $str .= "author = '$author' & ";
    }
    if($price != "*"){
        $str .= "price = '$price'";
    }
    $sql = "select * from books ".$str;
    $result = mysql_query($sql);
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_assoc($result)){
            $arr[] = $row;
        }
    }
    echo json_encode($arr);
}

mysql_close();
 ?>